<?php

namespace Admin\Controller;

use Think\Upload ;



class ArticleController extends BaseController {
    //文章显示
    public function lst(){
        $Article_cat =M('Article_cat');
        $map['state']=1;
        $cat = $Article_cat->field('name,cid,pid')->where($map)->select();
        $Nav = D('Nav');
        $cat_list= $Nav->list_level($cat);
        $this->assign('cat_list',$cat_list);

        
        $Article = M('Article');
        $count = $Article->count();
        $Page = new \Think\Page($count,5);
        $show = $Page->show();
        if (I('search')==''){
           
            $order ='classify';
        }else {
            $search =trim(I('search'));
            $map['title'] = array('like',"%$search%");
            $order ='createtime desc';

        }
        $Art = $Article->where($map)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('article',$Art);
        $this->assign('page',$show);
        $this->display();
        
    }
       
    //文章添加
    public function ad(){
        $Article_cat =M('Article_cat');
        $map['status'] = 1;
        $cat = $Article_cat->field('name,cid,pid')->where($map)->select();
        $Nav = D('Nav');
        $cat_list= $Nav->list_level($cat);
        $this->assign('cat_list',$cat_list);
        $Article = M('Article');
        if (IS_POST){
            $data = array(
                'title' => I('title'),
                'tags1' => I('tags'),
                'author' => I('author'),
                'content'=> I('content'),
                'status' => I('status'),
                'classify' => I('classify'),
                'createtime' => time(),
            );
            $id = $Article->max('id');
            $ar_id=$id+1;
            if ($_FILES['url']['tmp_name']!=""){
        
                $upload =new Upload();
                $upload->maxSize = 3145728;
                $upload->exts =array('jpg', 'gif', 'png', 'jpeg');
                $upload->rootPath  =  C('UPLOAD_PATH');
                $upload->savePath = 'article/ar/';
                $upload->saveName =  'ar_'.intval($ar_id);
                $upload->autoSub =  false;
                $info =$upload->upload();
                if (!$info){
                    $this->error($upload->getError());
                }else {
                    $data['url']=C('UPLOAD_PATH').$info['url']['savepath'].$info['url']['savename'];
                }
            }
            if ($Article->create($data)){
                if ($Article->add()){
                    $this->success('新增成功',U('lst'));
                }else {
                    $this->error('新增失败！');
                }
            }else {
                $this->error($Article->getError());
            }
            return ;
        }
        $this->display();
       
    }

    //文章修改
    public function edt($id){
        
        $Article_cat =M('Article_cat');
        $map['status'] = 1;
        $cat = $Article_cat->field('name,cid,pid')->where($map)->select();
        $Nav = D('Nav');
        $cat_list= $Nav->list_level($cat);
        $this->assign('cat_list',$cat_list);
        
        $Article = M('Article');
        $Art=$Article->find($id);
        $this->assign('Article',$Art);
        if (IS_POST){
            $data =array(
                'id'=>I('id'),
                'title'=>I('title'),
                'tags1'=>I('tags'),
                'status'=>I('status'),
                'classify'=>I('classify'),
                'author'=>I('author'),
                'createtime'=>time(),
                'content'=>I('content'),
            );
            if ($_FILES['url']['tmp_name']!=""){
                unlink($Art['url']);
                $upload =new Upload();
                $upload->maxSize = 3145728;
                $upload->exts =array('jpg', 'gif', 'png', 'jpeg');
                $upload->rootPath  =  C('UPLOAD_PATH');
                $upload->savePath = 'article/ar/';
                $upload->saveName =  'ar_'.intval(I('id'));
                $upload->autoSub =  false;
                $info =$upload->upload();
                if (!$info){
                    $this->error($upload->getError());
                }else {
                    $data['url']=C('UPLOAD_PATH').$info['url']['savepath'].$info['url']['savename'];
                }
            }else {
                $data['url']=$Art['url'];
            }
            if ($Article->create($data)){
                if ($Article->save()){
                    $this->success('修改成功',U('lst'));
                }else {
                    $this->error('修改失败！');
                }
            }else {
                $this->error($Article->getError());
            }
        
            return ;
        }
        
        
        $this->display();
                         
        
    } 
    
    
    
    //文章单删除
    public function del($id){
        $Article =M('Article');
        if ($Article->delete($id)){
            $this->success('删除成功！',U('lst'));
        }else {
            $this->error('删除失败');
        }
    

    }
    //文章全部删除
    public function del_both($id){
        
        $del_both = I('del_both');
        if ($del_both!==""){
            $del_id =implode(',', $del_both);
            $Article =M('Article');
            if ($Article->delete($del_id)){
                $this->success('删除成功！',U('lst'));
            }else {
                $this->error('删除失败');
            }
        }
        
    }
    
    
    //文章分类显示
    public function cat_lst(){
    

        $Article_cat = M('Article_cat');
        $Nav = D('Nav');
        $count = $Article_cat->count();
        $Page = new \Think\Page($count,20);
        $show = $Page->show();
        if (I('search')==''){
            
        }else {
            $search =trim(I('search'));
            $map['name']=array('like',"%$search%");  
            
        }
        $map['state']=1;
        $cat = $Article_cat->where($map)->order('createtime1 desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $cat_list = $Nav->list_level($cat);
        $this->assign('article_cat',$cat_list);
        $this->assign('page',$show);
        $this->display();
    }
    
//文章分类添加
   public function cat_ad(){
       $Article_cat = M('Article_cat');
       $Nav = D('Nav');
       $list =$Article_cat->select();
       $ar_list =$Nav-> list_level($list);
       $this->assign('ar_list',$ar_list);
       if (IS_POST){
           $data = array(
               'name' => I('name'),
               'tags' => I('tags'),
               'state' => I('state'),
               'pid'=>I('pid'),
               'temp'=>I('temp'),
               'createtime1' => time(),
           );
           if ($Article_cat->create($data)){
               if ($Article_cat->add()){
                   $this->success('新增成功',U('cat_lst'));
               }else {
                   $this->error('新增失败！');
               }
           }else {
               $this->error($Article_cat->getError());
           }
           return ;
       }
       
       $this->display();
   }
  //文章分类修改
  public function cat_edt($id){
      $Article_cat = M('Article_cat');
      $cat=$Article_cat->find($id);
      $this->assign('article_cat',$cat);
      $Nav = D('Nav');
      //获取全部分类
      $list =$Article_cat->select();
      $ar_list =$Nav->list_level($list);
      $this->assign('list',$ar_list);
      
      if (IS_POST){
          $data =array(
              'cid'=>I('id'),
              'name'=>I('name'),
              'tags'=>I('tags'),
              'state'=>I('state'),
              'temp'=>I('temp'),
              'createtime1'=>time(),
          );
          if ($Article_cat->create($data)){
              if ($Article_cat->save()){
                  $this->success('修改成功',U('cat_lst'));
              }else {
                  $this->error('修改失败！');
              }
          }else {
              $this->error($Article_cat->getError());
          }
      
          return ;
      }
      
      
      $this->display();
      
  }
    
    
    
  //文章分类单删除
  public function cat_del($id){
  

      $Article_cat =M('Article_cat');
      if ($Article_cat->delete($id)){
          $this->success('删除成功！',U('cat_lst'));
      }else {
          $this->error('删除失败');
      }
      
  
  }
  //文章分类全部删除
  public function cat_del_both(){
  
      $del_both = I('del_both');
      if ($del_both!==""){
          $del_id =implode(',', $del_both);
          $Article_cat =M('Article_cat');
          if ($Article_cat->delete($del_id)){
              $this->success('删除成功！',U('cat_lst'));
          }else {
              $this->error('删除失败');
          }
      }
  
  } 
    
    
  public function recomme_case(){
      $Picture =M('Article');
      $id=I('id');
      $is_recom=I('is_recom');
  
      $data=array(
          'id'=>$id,
          'is_recom'=>$is_recom,
      );
      if ($Picture->create($data)){
          if ($Picture->save()){
              echo 1;
          }else {
              echo 2;
          }
      }else {
          echo 2;
      }
  }
   
    

  public function banner_case(){
      $Picture =M('Article');
      $id=I('id');
      $is_banner=I('is_banner');
  
      $data=array(
          'id'=>$id,
          'is_banner'=>$is_banner,
      );
      if ($Picture->create($data)){
          if ($Picture->save()){
              echo 1;
          }else {
              echo 2;
          }
      }else {
          echo 2;
      }
  } 
    
    
    
  public function index_case(){
      $Picture =M('Article');
      $id=I('id');
      $index_banner=I('index_banner');
  
      $data=array(
          'id'=>$id,
          'index_banner'=>$index_banner,
      );
      if ($Picture->create($data)){
          if ($Picture->save()){
              echo 1;
          }else {
              echo 2;
          }
      }else {
          echo 2;
      }
  }
    

  public function index_recom(){
      $Picture =M('Article');
      $id=I('id');
      $index_banner=I('index_recom');
  
      $data=array(
          'id'=>$id,
          'index_recom'=>$index_banner,
      );
      if ($Picture->create($data)){
          if ($Picture->save()){
              echo 1;
          }else {
              echo 2;
          }
      }else {
          echo 2;
      }
  }
  
  public function index_hot(){
      $Picture =M('Article');
      $id=I('id');
      $index_banner=I('index_hot');
  
      $data=array(
          'id'=>$id,
          'index_hot'=>$index_banner,
      );
      if ($Picture->create($data)){
          if ($Picture->save()){
              echo 1;
          }else {
              echo 2;
          }
      }else {
          echo 2;
      }
  } 
    
}